package com.wust.service.impl;


import com.wust.common.ServerResponse;
import com.wust.dao.CarMapper;
import com.wust.dao.CheckupRecordMapper;
import com.wust.dao.UserCarMapper;
import com.wust.db.DataSource;
import com.wust.entity.Car;
import com.wust.entity.CheckupRecord;
import com.wust.entity.Data;
import com.wust.entity.UserCar;
import com.wust.service.IDataService;
import com.wust.service.IRecordService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lenovo on 2018/5/30.
 */
@Service("iRecordService")
public class RecordServiceImpl implements IRecordService {

    @Autowired
    private CheckupRecordMapper checkupRecordMapper;

    @Autowired
    private UserCarMapper userCarMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private IDataService iDataService;

    public ServerResponse<CheckupRecord> getLatestRecord(int userId){
        UserCar userCar = userCarMapper.selectByUserId(userId);
        if(userCar == null){
            return ServerResponse.createByErrorMessage("当前用户没有绑定设备");
        }
        CheckupRecord checkupRecord = assembleCheckupRecord(userId,userCar.getCarId());
        int resultCount = checkupRecordMapper.insertSelective(checkupRecord);
        if(resultCount > 0){
            CheckupRecord checkupRecord1 = checkupRecordMapper.selectLatestByUserId(userId);
            if(checkupRecord1 != null){
                return ServerResponse.createBySuccessMessageAndData("获取最新数据成功",checkupRecord1);
            }
        }
        return ServerResponse.createByErrorMessage("获取最新数据失败");
    }

    public CheckupRecord assembleCheckupRecord(int userId, int carId){
        Car car = carMapper.selectByPrimaryKey(carId);
        //分解数据值
        //"{\"HBP\": 66,\"LBP\": 64,\"HB\": 255}"
        Data data = iDataService.selectLatestDeviceData(car.getDeviceFirstId());
        String json = data.getDataValue();
        JSONObject jsonObject = new JSONObject(json);
//        "{\"TO\": 25.80,\"TA\": 27.45}"
        Data data1 = iDataService.selectLatestDeviceData(car.getDeviceSecondId());
        String json1 = data1.getDataValue();
        JSONObject jsonObject1 = new JSONObject(json1);
//        "{\"Left\": 0,\"Right\": 0}"
        Data data2 = iDataService.selectLatestDeviceData(car.getDeviceThirdId());
        String json2 = data2.getDataValue();
        JSONObject jsonObject2 = new JSONObject(json2);
//        "{\"AC\": 622}"
        Data data3 = iDataService.selectLatestDeviceData(car.getDeviceFourthId());
        String json3 = data3.getDataValue();
        JSONObject jsonObject3 = new JSONObject(json3);
        CheckupRecord checkupRecord = new CheckupRecord();
        System.out.println(jsonObject3.getInt("AC"));


        checkupRecord.setUserId(userId);
        checkupRecord.setCreateTime(data2.getCreateTime());
        checkupRecord.setHeartRate(jsonObject.getInt("HB"));//心率
        checkupRecord.setDiastolicPressure(jsonObject.getInt("LBP"));//舒张压
        checkupRecord.setSystolicPressure(jsonObject.getInt("HBP"));//收缩压
        checkupRecord.setTemperature(jsonObject1.getDouble("TO"));//体温
        checkupRecord.setLeftStress(jsonObject2.getInt("Left"));//左侧压力
        checkupRecord.setRightStress(jsonObject2.getInt("Right"));//右侧压力
        checkupRecord.setAlcoholConcentration(jsonObject3.getInt("AC"));//酒精浓度

        return checkupRecord;
    }

    public void Test(int userId, int carId){
        Car car = carMapper.selectByPrimaryKey(carId);
        System.out.println(car.getDeviceFourthId());
        Data data3 = iDataService.selectLatestDeviceData(car.getDeviceFourthId());
        String json3 = data3.getDataValue();
        JSONObject jsonObject3 = new JSONObject(json3);
        System.out.println(jsonObject3.getInt("AC"));
    }

//    public ServerResponse<CheckupRecord> getLatestRecordTest(int userId){
//        createData(userId);
//        CheckupRecord checkupRecord = checkupRecordMapper.selectLatestByUserId(userId);
//        if(checkupRecord != null){
//            return ServerResponse.createBySuccessMessageAndData("获取测试最新数据成功",checkupRecord);
//        }
//        return ServerResponse.createByErrorMessage("获取测试最新数据失败");
//    }
//
//    public ServerResponse createData(int userId){
//        CheckupRecord checkupRecord = new CheckupRecord();
//        checkupRecord.setUserId(2);
////        checkupRecord.setCreateTime(createDate());
//        checkupRecord.setCreateTime(Calendar.getInstance().getTime());
//        checkupRecord.setHeartRate(createNumber(20,60));//心率
//        checkupRecord.setDiastolicPressure(createNumber(30,61));//舒张压//低压区
//        checkupRecord.setSystolicPressure(createNumber(50,91));//收缩压//高压区
//        checkupRecord.setTemperature(0.3+createNumber(2,36));//体温
//        checkupRecordMapper.insertSelective(checkupRecord);
//        return ServerResponse.createBySuccess();
//    }
//
//    public int createNumber(int range,int range2){
//        Random rand = new Random();
//        return rand.nextInt(range) + range2;
//    }
//
//    public double createNumber(){
//        Random rand = new Random();
//        return (rand.nextInt(50)+30)*0.01;
//    }
//
//    public Date createDate() {
//        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            Date myDate2 = dateFormat2.parse("2018-06-04 20:"+createNumber(30,10)+":23");
//            return myDate2;
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        return Calendar.getInstance().getTime();
//    }






}
